﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Values
{
	using System.Collections.Generic;
	using System.Globalization;
	using System.Web.Mvc;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Validation;

	public class UserAlbumPhotoCreate : ValueBase<UserAlbumPhotoCreate>
	{
		// private fields
		private IList<AttachmentStore> _attachments;

		/// <summary>
		/// Initializes a new instance of the <see cref="UserAlbumPhotoCreate"/> class.
		/// </summary>
		public UserAlbumPhotoCreate()
		{
			this.AttachmentStoreService = Application.Resolve<IAttachmentStoreService>();
			this.UserAlbumService = Application.Resolve<IUserAlbumService>();

			var identity = User.Identity;

			if (identity == null)
			{
				return;
			}

			this.UserId = identity.Id;

			this.Attachment = new AttachmentCreate
			{
				Bind = Settings.Users.AlbumAttachmentFieldKey,
				Group = Settings.Users.AlbumAttachmentKey,
				TempId = identity.Id,
				UserId = identity.Id
			};

			this.Attachments = this.AttachmentStoreService.GetAll(new AttachmentStoreSpecification
			{
				Group = this.Attachment.Group,
				Key = this.Attachment.TempId.ToString(CultureInfo.InvariantCulture)
			});
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UserAlbumPhotoCreate"/> class.
		/// </summary>
		/// <param name="albumId">The album id.</param>
		public UserAlbumPhotoCreate(int? albumId) 
			: this()
		{
			if (albumId.HasValue)
			{
				this.AlbumId = albumId.Value;
			}
		}

		/// <summary>
		/// Gets or sets the attachment store repository.
		/// </summary>
		/// <value>The attachment store repository.</value>
		public IAttachmentStoreService AttachmentStoreService
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the user album repository.
		/// </summary>
		/// <value>The user album repository.</value>
		public IUserAlbumService UserAlbumService
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets or sets the user id.
		/// </summary>
		/// <value>The user id.</value>
		public int UserId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the album id.
		/// </summary>
		/// <value>The album id.</value>
		public int AlbumId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the attachment.
		/// </summary>
		/// <value>The attachment.</value>
		public AttachmentCreate Attachment
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the albums.
		/// </summary>
		/// <value>The albums.</value>
		public SelectList Albums
		{
			get
			{
				var albums = UserAlbumService.GetPaged(new UserAlbumSpecification
				{
					UserId = this.UserId, 
					Page = 1
				});

				return new SelectList(albums, "Id", "Title", this.AlbumId);
			}
		}

		/// <summary>
		/// Gets or sets the attachments.
		/// </summary>
		/// <value>The attachments.</value>
		public IList<AttachmentStore> Attachments
		{
			get
			{
				if (_attachments == null)
				{
					_attachments = new List<AttachmentStore>();
				}

				return _attachments;
			}
			set
			{
				_attachments = value;
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserAlbumPhotoCreate> Rules()
		{
			return base.Rules();
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}